/*
 * @Description: 弹窗修改数量
 * @Author: paddies
 * @Date: 2024-12-06 09:56:51
 * @LastEditors: paddies
 * @LastEditTime: 2024-12-06 10:28:27
 * @FilePath: /shared_components/lib/luntai-components/build-change-count/buildChangeCount.dart
 */
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:shared_components/build-button/buildButton.dart';
import 'package:shared_components/build-common/buildCommon.dart';

final TextEditingController countInputController = TextEditingController();
buildChangeCount({int count = 0, int min = 1, required Function onSubmit}) {
  // state.showCount.value = int.parse(state.count.toString());
  var myCount = 0.obs;
  myCount.value = count;

  countInputController.text = showText(myCount.value);

  // 设备安全高度
  final double statusBarHeight =
      MediaQuery.of(Get.overlayContext!).padding.bottom;
  // 设备安全高度
  Get.bottomSheet(
      isScrollControlled: true,
      Container(
        decoration: const BoxDecoration(
          color: Colors.white,
          // new Radius.circular(20.0)
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(20.0),
              topRight: Radius.circular(20.0)), //圆角
        ),
        height: 350,
        child: Column(
          children: [
            Container(
              width: double.infinity,
              height: 40,
              alignment: Alignment.center,
              child: Text(
                "购买数量",
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),
            ),
            Expanded(
                child: Container(
                    width: double.infinity,
                    height: double.infinity,
                    // color: Colors.red,
                    padding:
                        EdgeInsets.only(left: 10, right: 10, top: 0, bottom: 0),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Container(
                            width: double.infinity,
                            height: 60,
                            decoration: BoxDecoration(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(15.0)),
                              border: Border.all(
                                  color: Color(0xffc5c7ce), width: 1.5), // 边框样式
                            ),
                            child: CupertinoTextFormFieldRow(
                              padding: const EdgeInsets.only(
                                  top: 10, left: 10, right: 10),
                              style: const TextStyle(fontSize: 18),
                              controller: countInputController,
                              inputFormatters: [
                                FilteringTextInputFormatter.allow(
                                    RegExp(r'[0-9]')), // 只允许数字
                                _IntegerInputFormatter(), // 自定义Formatter，确保输入的是正整数
                              ],
                              placeholder: '请输入数量',
                              placeholderStyle: TextStyle(
                                  color: CupertinoColors.darkBackgroundGray
                                      .withOpacity(.7)),
                              keyboardType: TextInputType.emailAddress,
                              keyboardAppearance: Brightness.dark,
                              onChanged: (value) {
                                if (value == '' || value == '0') {
                                  value = '0';
                                }
                                myCount.value = int.parse(value);
                                // controller.state.loginPhone.value = value.toString();
                              },
                            ))
                      ],
                    ))),
            Container(
              width: double.infinity,
              height: 50,
              padding: EdgeInsets.only(left: 10, right: 10),
              child: buildButton(
                  title: "确定",
                  borderRadius: 3,
                  onClick: () async {
                    if (myCount.value < min) {
                      EasyLoading.showToast("最小数量为" + showText(min));
                      return;
                    }
                    onSubmit(myCount.value);
                    // await doUpdateCard([
                    //   {"num": state.showCount.value, "skuId": skuId}
                    // ]);
                    // initData();
                    // state.count.value = state.showCount.value;
                    // getCalFreight();
                    // getAllCost();
                    Get.back();
                  },
                  colors: [Color(0xff3e4beb), Color(0xff3e4beb)]),
            ),
            SizedBox(
              height: statusBarHeight + 40,
            )
          ],
        ),
      ));
}

class _IntegerInputFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    // 移除非数字字符
    String text = newValue.text.replaceAll(RegExp(r'[^0-9]'), '');
    // 确保第一个字符不是0，除非字符串是'0'代表0本身
    if (text.isNotEmpty && text.startsWith('0') && text != '0') {
      text = oldValue.text;
    }
    // if (text == '0') {
    //   EasyLoading.showToast("最小数量为1");
    //   text = '1';
    // }
    return newValue.copyWith(text: text);
  }
}
